$(document).ready(function () {
    var host = 'http://120.55.161.117:8080';
    var url1='/portal/index/hotbrandpic';//首页开头热门品牌图片加载
    var url2='/portal/index/hot/series/car';//热门车，分四类
    var url3='/portal/index/new/car/suv';//新车 suv模块，不涉及车价
    var url4='/portal/index/car/by/price';//按价格排列
    var url5 = '/portal/index/pic/show';//开头右侧图片动态展示
    var url6='/portal/index/search/brand';//保养搜索 品牌
    var url7='/portal/index/search/series';//保养搜索 车型
    var url8 = '/portal/index/series/ad/show';//八个车型广告位
    var url9 = '/portal/index/carousel/article';//新闻轮播展示
    var url10 = '/portal/index/all/area/headline';//全局新闻头条
    var url11 = '/portal/index/global/news';//全局新闻
    var url12 = '/portal/index/factory/set';//厂商动态 & 测评
    var url13 = '/portal/index/new/car/appear'; // 新车上市
    var url14 = '/portal/index/say/things';//说客
    var url15 = '/portal/index/area/index/top'; //分站 新闻展示
    var url16 = '/portal/index/area/news/headline'; //分站，头条展示
    var url17 = '/portal/index/area/news/article'; //分站，所有新闻
    var url18 = '/portal/index/area/hot/series'; //分站，热门车型，猜你喜欢&价格
    var url19 = '/portal/index/area/recommend/producer';//推荐商家
    var url20 = '/portal/index/area/famous/interviews'; //人物访谈
    var url21 = '/portal/index/area/sales'; //促销
    var url22 = '/portal/index/all/buying/car'; //大家都在买什么车
    var url23 = '/portal/index/global/ad'; //全局广告，包括新闻轮播图位置的广告
    var url24 = '/portal/index/area/ad'; //区域广告，不包括热销车
    var url25 = '/portal/index/hot/saling/car'; // 区域热销车
    //页面设置默认area_id为1,江苏/南京
    var area_id =1;
    $('#indexTopPic_1').show().siblings().hide();
    $('#indexTopPicDiv').parent().parent().find('div[class="car-right"]').each(function () {
                    $(this).hide();
                });
    $('#carRight_1').show();
    $('#indexTopPicDiv').children().each(function () {
        if($(this).attr('topPic')){
            var pic_id = $(this).attr('topPic');
            $(this).mousemove(function () {
                $('#indexTopPic_'+pic_id).show().siblings().hide();
                $('#indexTopPicDiv').parent().parent().find('div[class="car-right"]').each(function () {
                    $(this).hide();
                });
                $('#carRight_'+pic_id).show();
            })
        }
    });

    $('#areaHotSeriesLabel').children().each(function () {
        $(this).mousemove(function () {
            var index = $(this).index();
            $('#areaHotSeries').children().eq(index).show().siblings().hide();
        });
    });
    $('#areaHotSeries').children().eq(0).show().siblings().hide();

    $('#allWantsBuysLabel').children().each(function () {
        var index = $(this).index();
        $(this).mousemove(function () {
            $('#allWantsBuys').children().eq(index).show().siblings().hide();
        })
    });
    $('#allWantsBuys').children().eq(0).show().siblings().hide();

    $('#pageSaleLabel').children().each(function () {
        $(this).click(function () {
            var index = $(this).index();
            $(this).addClass('active').siblings().removeClass('active');
            if(index==0){
                $('#pageSale_1' ).show();
                $('#pageSale_2' ).hide();
            }else{
                $('#pageSale_2' ).show();
                $('#pageSale_1' ).hide();
            }
        })
    });
    var active_index = $('#pageSaleLabel').find('span[class="active"]').index();
            if(active_index==0){
                $('#pageSale_1' ).show();
                $('#pageSale_2' ).hide();
            }else{
                $('#pageSale_2' ).show();
                $('#pageSale_1' ).hide();
            }


    top_frame_pic_ajax(url1,'#indexTopPic_1');
   function top_frame_pic_ajax(url,id) {
       $.ajax({
           url: host + url,
           type: 'get',
           async: false,
           datatype: 'json',
           success: function (data) {
               if (data[0]) {
                   for (var i = 0; i < data.length&& i < 12; i++) {
                       var t = "<li><a href='#'><img src='" + host + data[i]["pic"] + "' width='70px' height='55px'/><br><span class='hotBrandPic'>"+data[i]["name"]+"</span></a></li>";
                       $(id).append(t);
                       //$("#brandPic li").eq(i).html(t);
                   }
               }
           }
       });
   }
    $.ajax({
        url:host +url2,
        type:'get',
        async: true,
        dataType:'json',
        success:function (data) {
            console.log(data);
            if(data){
                for(var i=0;i<data.length; i++){
                   var t = "<span class='indexTopCar'><a href='#'>"+ data[i]['name'] +"</a> </span>";
                    $('#hotSeriesCar_'+data[i]['type']).append(t)
                }
            }
        }
    });

    $.ajax({
        url:host+url3,
        type:'get',
        async: true,
        dataType:'json',
        success:function (data) {
            console.log(data);
            if(data){
                for(var i=0;i<data.length; i++){
                    var t = "<span class='indexTopSeriesCar'><a href='#'>"+ data[i]['name'] +"</a></span>";
                    $('#newCarSuv_' + data[i]['type']).append(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url4,
        type:'get',
        async:true,
        dataType:'json',
        success:function (data) {
            if(data){
                for(var i=0; i<data.length; i++){
                    var t = "<span class='indexTopSeriesCar'><a href='#'>"+ data[i]['name'] +"</a></span>"
                    $('#indexTopPic_'+data[i]['type']).children().eq(data[i]['level']).append(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url5,
        type: 'get',
        dataType: 'json',
        async: true,
        success:function (data) {
            for(var i=0; i<data.length; i++) {
                var t = "<a href='#'><img src='" + host + data[i]['pic'] + "'></a><h2>"+ data[i]['price'] +"</h2><span>"+ data[i]['name'] +"</span><p>官网 图片 配置</p>";
                $('#carRight_' + data[i]['type']).append(t);
            }
        }
    });

    $.ajax({
       url: host + url6,
       type: 'get',
       async: true,
       dataType: 'json',
       success: function (data) {
                $('#search_brand').empty();
                $("<option value=''>请选择品牌</option>").appendTo($('#search_brand'));
               for (var i = 0; i < data.length; i++) {
                   var t = "<option value='"+data[i]['id']+"'>"+data[i]['name']+"</option>";
                   $('#search_brand').append(t);
                   //$("#brandPic li").eq(i).html(t);
               }
       }
    });
    $('#search_series').attr({disabled:true});
    $('#search_brand').change(function () {
        if($('#search_brand').val()==""){
            $('#search_series').attr({disabled:true});
        }
        else {
            $('#search_series').removeAttr('disabled');
            $.ajax({
                   url: host + url7,
                   type: 'get',
                   async: false,
                   dataType: 'json',
                   data:{'brandid':$('#search_brand').val()},
                   success: function (data) {
                            $('#search_series').empty()
                           for (var i = 0; i < data.length; i++) {
                               var t = "<option value='"+data[i]['id']+"'>"+data[i]['name']+"</option>";
                               $('#search_series').append(t);
                               //$("#brandPic li").eq(i).html(t);
                           }
                   }
            });
        }
    });

    $.ajax({
        url: host + url8,
        dataType: 'json',
        type: 'get',
        async:true,
        success:function (data) {
            for(var i=0;i<data.length;i++){
                var t = "<a href='#'><img width='110px' height='70' src='"+host +  data[i]['pic'] +"'><p>"+ data[i]['name'] +"</p></a>";
                $('#seriesAd').children().eq(data[i]['type']).html('');
                $('#seriesAd').children().eq(data[i]['type']).append(t);
            }
        }

    });

    $.ajax({
        url: host + url9,
        dataType: 'json',
        method:'GET',
        async:true,
        success:function (data) {
            if(data){
                console.log('carousel');
                console.log(data);
                for(var i=0;i<data.length;i++){
                    var page = parseInt((data[i]['position'] - 1) / 5 + 1);
                    var position = parseInt((data[i]['position'] - 1) % 5);
                    if(position == 0){
                        var t = "<a href='carPic.htm?id="+data[i]['id']+"'><img src='"+ host + data[i]['pic'] +"' width='490' height='330'><p>"+ data[i]['title'] +"</p></a>";
                    }else{
                        var t = "<a href='carPic.htm?id="+data[i]['id'] +"'><img src='"+ host + data[i]['pic'] +"' width='240' height='160'><p>"+ data[i]['title'] +"</p></a>";
                    }
                    if(page == 1 || page == 4){
                        $('#page_' + page + '_1').find('div[class="car_pic_txt"]').eq(position).html(t);
                        $('#page_' + page + '_2').find('div[class="car_pic_txt"]').eq(position).html(t);
                    }else {
                        $('#page_' + page).find('div[class="car_pic_txt"]').eq(position).html(t);
                    }
                }
            }
        }
    });

    $.ajax({
        url:host + url10,
        type:'get',
        dataType: 'json',
        async: true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t ="<h2>"+ data[i]['title'] +"</h2>";
                    $('#allAreaHead').children().eq(i).attr({href: "carPic.htm?id="+data[i]['id']});
                    $('#allAreaHead').children().eq(i).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host  + url11,
        dataType:'json',
        method:'GET',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<a href='carPic.htm?id="+data[i]['id'] +"'>"+ data[i]['title'] +"</a>";
                    $('#globalNewsSet_'+data[i]['type']).children().eq(data[i]['level']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url12,
        method:'GET',
        dataType:'json',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length; i++) {
                    if (data[i]['type'] == 0 && data[i]['kind'] == 1){
                        var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><h2>" + data[i]['title'] + "</h2></a>";
                    }else if(data[i]['kind'] == 1) {
                        var t = "<i class='fa fa-caret-right'></i>" + data[i]['title'];
                    }else{
                        var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><span>"+ (data[i]['type']+1) +"</span>"+ data[i]['title'] +"</a>";
                    }
                    $('#factorySet_' + data[i]['kind']).children().eq(data[i]['type']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url13,
        method: 'GET',
        async:true,
        dataType:'json',
        success:function (data) {
            if(data){
                for(var i =0;i<data.length; i++){
                    var t = "<img src='"+ host + data[i]['pic'] +"'><h2>" + data[i]['name'] + "</h2><a href='"+ "carPic.htm?id="+data[i]['id'] +"'>[详细]</a></p>";
                    $('#newCarAppear').children().eq(data[i]['type']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url14,
        method:'GET',
        dataType:'json',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><img src='"+ host+ data[i]['pic'] +"'><p>"+ data[i]['title'] +"</p><a>";
                    $('#sayThings').children().eq(data[i]['type']).html(t)
                }
            }
        }
    });

    $.ajax({
        url: host + url15,
        method : 'GET',
        dataType: 'json',
        async: true,
        data:{"area_id": area_id},
        success:function (data) {
            if(data){
                for(var i=0; i < data.length; i ++){
                    if(data[i]['type'] == 1){
                        var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><img src='"+ host + data[i]['pic'] +"' alt='' width='256' height='300'><div><h2>"+ data[i]['title'] +"</h2></div></a>"
                    }else{
                        var t = "<a href='" + "carPic.htm?id="+data[i]['id'] + "'><img src='"+ host + data[i]['pic'] +"' alt='' width='256' height='156'><div><h2>"+ data[i]['title'] +"</h2></div></a>"
                    }
                    $('#areaTop').children().eq(data[i]['type']-1).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url16,
        dataType: 'json',
        data:{"area_id": area_id},
        method:'GET',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    $('#areaNewsHead_' + data[i]['type']).children().eq(data[i]['level']).attr({href:"carPic.htm?id="+data[i]['id']});
                    $('#areaNewsHead_' + data[i]['type']).children().eq(data[i]['level']).text(data[i]['title']);
                }
            }
        }
    });

    $.ajax({
        url: host + url17,
        dataType:'json',
        method: 'GET',
        data:{'area_id':area_id},
        async:true,
        success:function (data) {
            console.log(data);
            if(data){
                for(var i=0;i<data.length;i++){
                    var t= "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><i class='fa fa-circle'></i>"+ data[i]['title'] +"</a>";
                    $('#areaNews_' + data[i]['level']).children().eq(data[i]['type']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url18,
        dataType : 'json',
        method: 'GET',
        data:{'area_id': area_id},
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<td><img src='"+ host + data[i]['logo'] +"' width='28px' height='28px'></td><td>" + data[i]['name'] + "</td><td class='red'>"+ data[i]['price'] +"万</td><td class='delete'>" + data[i]['pre_price'] + "万</td><td>" + data[i]['producer'] + "</td>";
                    $('#areaHotSeries').children().eq(data[i]['type']-1).children().children().eq(data[i]['level']).html(t)
                }
            }
        }
    });

        var rp_tx = new Array(4);
    $.ajax({
        url: host + url19,
        type: 'get',
        async: true,
        dataType: 'json',
        data: {'area_id':area_id},
        success: function (data) {
            console.log(data);
            for(var i=0;i<4;i++){
                rp_tx[i] = "<div class='list-box'>";
                for(var j=0;j<16;j++){
                    var n=i*16+j;
                    if(data[n]){
                         rp_tx[i] +="<div class='box-1 '><div class='box-2'><img src='"+host+data[n]['pic']+"' width='45px' height='45px'></div><div class='box-3'><span><a href='#'>"+data[n]['name']+"</a></span><span>"+data[n]['del']+"</span> </div><div class='box-4'><span class='red'>"+data[n]['type']+"</span><span>雨花台</span> </div> </div>"
                    }else{
                         rp_tx[i] +="<div class='box-1 '><div class='box-2'><img src=''></div><div class='box-3'><span><a href='#'></a></span><span></span> </div><div class='box-4'><span class='red'></span><span></span> </div> </div>"
                    }
                }
                $('#list-zbb').append(rp_tx[i]+"</div>");
            }
                $('#list-zbb').prepend(rp_tx[3]+"</div>");
                $('#list-zbb').append(rp_tx[0]+"</div>");
        }
    });

    $.ajax({
        url: host + url20,
        dataType:'json',
        method: 'GET',
        data:{"area_id": area_id},
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><img src='" + host + data[i]['pic']+  "'><p>"+ data[i]['title'] +"</p></a>";
                    $('#famousInterview').children().eq(data[i]['type']).html(t);
                }
            }
        }
    });

    $.ajax({
        url :host + url21,
        dataType : 'json',
        method: 'GET',
        data:{'area_id': area_id},
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<a href='"+ "carPic.htm?id="+data[i]['id'] +"'><i class='fa fa-circle'></i>"+ data[i]['title'] +"</a>";
                    $('#pageSale_' + data[i]['type']).children().eq(data[i]['level']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url22,
        method:'GET',
        dataType:'json',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    if(data[i]['level']<3){
                        var t = "<span class='active'>"+ (data[i]['level']+1) +"</span><a href='#'>"+ data[i]['name'] +"</a>"
                    }else{
                        var t="<span>"+ (data[i]['level']+1) +"</span><a href='#'>"+ data[i]['name'] +"</a>";
                    }
                    $('#allWantsBuys').children().eq(data[i]['type']).children().eq(data[i]['level']).html(t);
                }
            }
        }
    });

    $.ajax({
        url:host + url23,
        method:'GET',
        dataType:'json',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    if(data[i]['type']){
                        if(data[i]['type']>6 && data[i]['type']<11){
                            $('#globalAd_' + data[i]['type']).attr({src:host+data[i]['pic'], width: data[i]['width'], height: data[i]['height'], alt: data[i]['name']});
                            $('#globalAd_' + data[i]['type']).parent().attr({href:data[i]['link']});
                        }else{
                            $('#globalAd_' + data[i]['type']).attr({src:host+data[i]['pic'], width: data[i]['width'], height: data[i]['height']});
                            $('#globalAd_' + data[i]['type']).parent().attr({href:data[i]['link']});
                        }
                    }else{
                        var page = parseInt((data[i]['position'] - 1) / 5 + 1);
                        var position = parseInt((data[i]['position'] - 1) % 5);
                        if(position == 0){
                            var t = "<a href='"+ data[i]['link'] +"'><img src='"+ host + data[i]['pic'] +"' width='490' height='330'><p>"+ data[i]['name'] +"</p></a>";
                        }else{
                            var t = "<a href='"+ data[i]['link'] +"'><img src='"+ host + data[i]['pic'] +"' width='240' height='160'><p>"+ data[i]['name'] +"</p></a>";
                        }
                        if(page == 1 || page == 4){
                            $('#page_' + page + '_1').find('div[class="car_pic_txt"]').eq(position).html(t);
                            $('#page_' + page + '_2').find('div[class="car_pic_txt"]').eq(position).html(t);
                        }else {
                            $('#page_' + page).find('div[class="car_pic_txt"]').eq(position).html(t);
                        }
                    }
                }
            }
        }
    });

    $.ajax({
        url:host + url24,
        dataType: 'json',
        data:{"area_id": area_id},
        method:'GET',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    if(data[i]['type']>6 && data[i]['type']<11){
                        $('#areaAd_' + data[i]['type']).attr({src:host+data[i]['pic'], width: data[i]['width'], height: data[i]['height'], alt: data[i]['name']});
                        $('#areaAd_' + data[i]['type']).parent().attr({href:data[i]['link']});
                    }else{
                        $('#areaAd_' + data[i]['type']).attr({src:host+data[i]['pic'], width: data[i]['width'], height: data[i]['height']});
                        $('#areaAd_' + data[i]['type']).parent().attr({href:data[i]['link']});
                    }
                }
            }
        }
    });

    $.ajax({
        url:host + url25,
        dataType: 'json',
        data:{"area_id": area_id},
        method:'GET',
        async:true,
        success:function (data) {
            if(data){
                for(var i=0;i<data.length;i++){
                    var t = "<a href='"+ data[i]['link'] +"'><img src='"+ host + data['pic'] +"'><p>"+ data[i]['name'] +"</p></a>";
                    $('#areaHotCar').children().eq(data[i]['position']).html(t);
                }
            }
        }
    });
});